package com.zl.zlproject.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zl.zlproject.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 郑磊 on 2018/6/7.
 * <p>
 * 普通的条目
 */

public class NormalItemView extends LinearLayout {


    @BindView(R.id.iv_icon)
    ImageView mIvIcon;
    @BindView(R.id.tv_left)
    TextView mTvLeft;
    @BindView(R.id.tv_right)
    TextView mTvRight;
    @BindView(R.id.iv_to_right)
    ImageView mIvToRight;
    @BindView(R.id.line)
    View mLine;

    public NormalItemView(Context context) {
        this(context, null);
    }

    public NormalItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NormalItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.item_normal, this);
        ButterKnife.bind(this);

        //获取全局属性
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.NormalItemView);
        String textLeft = ta.getString(R.styleable.NormalItemView_textLeft);
        boolean iconToggle = ta.getBoolean(R.styleable.NormalItemView_iconToggle, true);
        String textRight = ta.getString(R.styleable.NormalItemView_textRight);
        int leftTextColor = ta.getColor(R.styleable.NormalItemView_textLeftColor, context.getResources().getColor(R.color.tvColorgray2));
        int rightTextColor = ta.getColor(R.styleable.NormalItemView_textRightColor, context.getResources().getColor(R.color.tvColorgray5));
        int iconResId = ta.getResourceId(R.styleable.NormalItemView_imageSrc, R.mipmap.icon_me);
        boolean ivRighttoggle = ta.getBoolean(R.styleable.NormalItemView_imageRightToggle, true);
        boolean showLine =ta.getBoolean(R.styleable.NormalItemView_showLine,true);
        ta.recycle();



        //设置属性
        mIvIcon.setImageResource(iconResId);
        mIvIcon.setVisibility(iconToggle ? View.VISIBLE : View.GONE);

        mTvLeft.setText(textLeft);
        mTvLeft.setTextColor(leftTextColor);


        mTvRight.setText(textRight);
        mTvRight.setTextColor(rightTextColor);

        mIvToRight.setVisibility(ivRighttoggle ? View.VISIBLE : View.GONE);
        mLine.setVisibility(showLine?View.VISIBLE:View.GONE);
    }


    public void setText(String text) {
        mTvRight.setText(text);
    }


    public String getText() {
        return mTvRight.getText().toString().trim();
    }
}
